﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.JShop.UI;
using System.Data;
using CNVP.Framework;
using CNVP.Framework.Utils;
using CNVP.Framework.Helper;

namespace CNVP.JShop.Web.Admin
{
    public partial class OrderList : AdminPage
    {
        BLL.CNVP_Order bll = new BLL.CNVP_Order();
        public int rplistCount = 0;
        public int olbid;
        public int PageIndex = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 94)) return;//判断页面权限
            olbid = BasePage.GetInt("olbid", -1);

            if (!string.IsNullOrEmpty(Request.QueryString["Page"]) && Public.IsNumber(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            if (!string.IsNullOrEmpty(Request.QueryString["olbid"]))
            {
                olbid = BasePage.GetInt("olbid", -1);
            }
            if (!IsPostBack)
            {
                ViewState["SearchType"] = "0";
                OrderState();
                StartLoad(PageIndex);
                btDelete.Attributes.Add("onclick", "if(ifCheck()){return confirm('确定取消该记录吗?');}else{alert('请选择记录！');return false;};");
            }
        }

        /// <summary>
        /// PageNavigator1_PageChange 的摘要说明
        /// 分页加载列表函数
        /// </summary>
        protected void StartLoad(int PageIndex)
        {
            int i, j;
            DataTable dt = null;
            int pageNum = 20;
            SQLConditionInfo[] st = new SQLConditionInfo[7];
            st[0] = new SQLConditionInfo("@olbid", olbid);
            st[1] = new SQLConditionInfo("@oUserAdminType", "1");
            st[2] = new SQLConditionInfo("@key", Public.FilterSql(txtkey.Text.Trim()));
            st[3] = new SQLConditionInfo("@olbid", ddlOrderState.SelectedValue);
            st[4] = new SQLConditionInfo("@BeginDate", Request.Form["BeginDate"]);
            st[5] = new SQLConditionInfo("@EndDate", Request.Form["EndDate"]);
            st[6] = new SQLConditionInfo("@SearchType", ViewState["SearchType"]);
            dt = bll.GetPage(this.GetType().Name, PageIndex, pageNum, out i, out j, st);
            if (dt != null)
            {
                rplistCount = dt.Rows.Count;//行数
                //----------------------------------------添加列------------------------------------------------
                dt.Columns.Add("oOrderNum", typeof(string));
                dt.Columns.Add("oAddTime", typeof(string));
                dt.Columns.Add("oOrderState", typeof(string));
                dt.Columns.Add("oOrderStateNO", typeof(string));
                dt.Columns.Add("oPayWay", typeof(string));
                dt.Columns.Add("oBuyerID", typeof(string));
                //----------------------------------------添加列结束--------------------------------------------
                for (int k = 0; dt.Rows.Count > k; k++)
                {
                    string osNo = dt.Rows[k]["OrderState"].ToString();
                    if (osNo == "2" || osNo == "4" || osNo == "5" || osNo == "7" || osNo == "8" || osNo == "9")
                    {
                        dt.Rows[k]["oOrderNum"] = "<font color=\"#CCCCCC\">" + dt.Rows[k]["OrderNum"] + "</font>";
                    }
                    else
                    {
                        dt.Rows[k]["oOrderNum"] = dt.Rows[k]["OrderNum"];
                    }
                    dt.Rows[k]["oAddTime"] = Convert.ToDateTime(dt.Rows[k]["AddTime"]).ToShortDateString();
                    dt.Rows[k]["oOrderState"] = BLL.BLLOrder.getorderzt(Convert.ToInt32(dt.Rows[k]["OrderState"]));
                    dt.Rows[k]["oOrderStateNO"] = dt.Rows[k]["OrderState"];//数字
                    string _ColumnName = "";
                    var model_Column=new BLL.CNVP_Column().GetSingle_CNVP_ColumnById(Convert.ToInt32(dt.Rows[k]["PayWay"]));
                    if (model_Column != null)
                    {
                        _ColumnName = model_Column.ColumnName;
                    }
                    dt.Rows[k]["oPayWay"] = _ColumnName;
                    string _UserName = "";
                    var model_Users=new BLL.CNVP_Users().GetSingle_CNVP_UsersById(Convert.ToInt32(dt.Rows[k]["BuyerID"]));
                    if (model_Users != null)
                    {
                        _UserName = model_Users.UserName;
                    }
                    dt.Rows[k]["oBuyerID"] = _UserName; 
                }
            }
            rpList.DataSource = dt;
            rpList.DataBind();
            paging.InnerHtml = Paginator.Pagination(PageIndex, i, j);
        }

        protected void rpList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //方法一
            if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
            {
                Label lbOrderStateNO = (Label)e.Item.FindControl("lbOrderStateNO");
                string osNo = lbOrderStateNO.Text;
                HyperLink HLinkoOrderNum = (HyperLink)e.Item.FindControl("HLinkoOrderNum");
                CheckBox cbCheck = (CheckBox)e.Item.FindControl("cbCheck");
                if (osNo == "2" || osNo == "4" || osNo == "5" || osNo == "7" || osNo == "8" || osNo == "9")
                {
                    cbCheck.Enabled = false;
                    HLinkoOrderNum.NavigateUrl = "OrderShow.aspx?olbid=" + olbid + "&Page=" + PageIndex + "&ID=" + cbCheck.ToolTip;
                }
                else if (osNo == "9")
                {
                    HLinkoOrderNum.NavigateUrl = "OrderShow.aspx?olbid=" + olbid + "&Page=" + PageIndex + "&ID=" + cbCheck.ToolTip;
                }
                else
                {
                    HLinkoOrderNum.NavigateUrl = "OrderEdit.aspx?olbid=" + olbid + "&Page=" + PageIndex + "&ID=" + cbCheck.ToolTip;
                }
            }
        }

        protected void rpList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());
            if (e.CommandName == "link1")
            {
                Response.Redirect("PrintBuy.aspx?ID=" + id);
            }
            if (e.CommandName == "link2")
            {
                Response.Redirect("PrintDelivery.aspx?ID=" + id);
            }
            if (e.CommandName == "link3")
            {
                Response.Redirect("PrintDeliveryBuy.aspx?ID=" + id);
            }
            if (e.CommandName == "link4")
            {
                Response.Redirect("PrintExpress.aspx?ID=" + id);
            }

        }

        protected void btDelete_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.rpList.Items.Count; i++)
            {
                CheckBox cbCheck = (CheckBox)this.rpList.Items[i].FindControl("cbCheck");
                if (cbCheck.Checked == true)
                {
                    int code = int.Parse(cbCheck.ToolTip);
                    Model.CNVP_Order model = new Model.CNVP_Order();
                    model.OrderState = 9;
                    model.ID = code;
                    bll.UpdateOrderState(model);
                }
            }
            StartLoad(PageIndex);
        }

        private void OrderState()
        {
            string html = "";
            string[] orderzt = { "等待付款", "准备发货(到付或已付)", "配送中", "", "确认收货", "付款成功", "", "已结算", "退货单", "已取消" };
            if (olbid==-1)
			{
				html+="<b><a href='orderlist.aspx' style='COLOR: #009966\'>所有订单</a></b> ";
			}
			else
			{
				html+="<a href='orderlist.aspx'>所有订单</a> ";
			}
			for (int i = 0;i<10;i++)
			{
				if(i==olbid)
				{
					html+="<b><a href='orderlist.aspx?olbid="+i+"' style='COLOR: #009966\'>"+orderzt[i]+"</a></b> ";
				}
				else
				{
                    html += "<a href='orderlist.aspx?olbid=" + i + "'>" + orderzt[i] + "</a> ";
				}
			}
            OLb.Text = html;
        }

        protected void btHighSearch_Click(object sender, EventArgs e)
        {
            ViewState["SearchType"] = "1";
            StartLoad(PageIndex);
        }
    }
}
